home *** CD-ROM | disk | FTP | other *** search
/ Mac Easy 2010 May / Mac Life Ubuntu.iso / casper / filesystem.squashfs / usr / lib / python2.6 / dist-packages / PIL / ImageOps.pyc (.txt) < prev    next >
Encoding:
Python Compiled Bytecode  |  2009-04-20  |  7.7 KB  |  265 lines

  1. # Source Generated with Decompyle++
  2. # File: in.pyc (Python 2.6)
  3.  
  4. import Image
  5. import operator
  6.  
  7. def _border(border):
  8.     if type(border) is type(()):
  9.         if len(border) == 2:
  10.             (left, top) = (right, bottom) = border
  11.         elif len(border) == 4:
  12.             (left, top, right, bottom) = border
  13.         
  14.     else:
  15.         left = top = right = bottom = border
  16.     return (left, top, right, bottom)
  17.  
  18.  
  19. def _color(color, mode):
  20.     if Image.isStringType(color):
  21.         import ImageColor
  22.         color = ImageColor.getcolor(color, mode)
  23.     
  24.     return color
  25.  
  26.  
  27. def _lut(image, lut):
  28.     if image.mode == 'P':
  29.         raise NotImplementedError('mode P support coming soon')
  30.     image.mode == 'P'
  31.     if image.mode in ('L', 'RGB'):
  32.         if image.mode == 'RGB' and len(lut) == 256:
  33.             lut = lut + lut + lut
  34.         
  35.         return image.point(lut)
  36.     raise IOError, 'not supported for this image mode'
  37.  
  38.  
  39. def autocontrast(image, cutoff = 0, ignore = None):
  40.     '''Maximize image contrast, based on histogram'''
  41.     histogram = image.histogram()
  42.     lut = []
  43.     for layer in range(0, len(histogram), 256):
  44.         h = histogram[layer:layer + 256]
  45.         if ignore is not None:
  46.             
  47.             try:
  48.                 h[ignore] = 0
  49.             except TypeError:
  50.                 for ix in ignore:
  51.                     h[ix] = 0
  52.                 
  53.             
  54.  
  55.         None<EXCEPTION MATCH>TypeError
  56.         if cutoff:
  57.             n = 0
  58.             for ix in range(256):
  59.                 n = n + h[ix]
  60.             
  61.             cut = n * cutoff / 100
  62.             for lo in range(256):
  63.                 if cut > h[lo]:
  64.                     cut = cut - h[lo]
  65.                     h[lo] = 0
  66.                 else:
  67.                     h[lo] = h[lo] - cut
  68.                     cut = 0
  69.                 if cut <= 0:
  70.                     break
  71.                     continue
  72.             
  73.             cut = n * cutoff / 100
  74.             for hi in range(255, -1, -1):
  75.                 if cut > h[hi]:
  76.                     cut = cut - h[hi]
  77.                     h[hi] = 0
  78.                 else:
  79.                     h[hi] = h[hi] - cut
  80.                     cut = 0
  81.                 if cut <= 0:
  82.                     break
  83.                     continue
  84.             
  85.         
  86.         for lo in range(256):
  87.             if h[lo]:
  88.                 break
  89.                 continue
  90.         
  91.         for hi in range(255, -1, -1):
  92.             if h[hi]:
  93.                 break
  94.                 continue
  95.         
  96.         if hi <= lo:
  97.             lut.extend(range(256))
  98.             continue
  99.         scale = 255 / (hi - lo)
  100.         offset = -lo * scale
  101.         for ix in range(256):
  102.             ix = int(ix * scale + offset)
  103.             if ix < 0:
  104.                 ix = 0
  105.             elif ix > 255:
  106.                 ix = 255
  107.             
  108.             lut.append(ix)
  109.         
  110.     
  111.     return _lut(image, lut)
  112.  
  113.  
  114. def colorize(image, black, white):
  115.     '''Colorize a grayscale image'''
  116.     if not image.mode == 'L':
  117.         raise AssertionError
  118.     black = _color(black, 'RGB')
  119.     white = _color(white, 'RGB')
  120.     red = []
  121.     green = []
  122.     blue = []
  123.     for i in range(256):
  124.         red.append(black[0] + i * (white[0] - black[0]) / 255)
  125.         green.append(black[1] + i * (white[1] - black[1]) / 255)
  126.         blue.append(black[2] + i * (white[2] - black[2]) / 255)
  127.     
  128.     image = image.convert('RGB')
  129.     return _lut(image, red + green + blue)
  130.  
  131.  
  132. def crop(image, border = 0):
  133.     '''Crop border off image'''
  134.     (left, top, right, bottom) = _border(border)
  135.     return image.crop((left, top, image.size[0] - right, image.size[1] - bottom))
  136.  
  137.  
  138. def deform(image, deformer, resample = Image.BILINEAR):
  139.     '''Deform image using the given deformer'''
  140.     return image.transform(image.size, Image.MESH, deformer.getmesh(image), resample)
  141.  
  142.  
  143. def equalize(image, mask = None):
  144.     '''Equalize image histogram'''
  145.     if image.mode == 'P':
  146.         image = image.convert('RGB')
  147.     
  148.     h = image.histogram(mask)
  149.     lut = []
  150.     for b in range(0, len(h), 256):
  151.         histo = filter(None, h[b:b + 256])
  152.         if len(histo) <= 1:
  153.             lut.extend(range(256))
  154.             continue
  155.         step = (reduce(operator.add, histo) - histo[-1]) / 255
  156.         if not step:
  157.             lut.extend(range(256))
  158.             continue
  159.         n = step / 2
  160.         for i in range(256):
  161.             lut.append(n / step)
  162.             n = n + h[i + b]
  163.         
  164.     
  165.     return _lut(image, lut)
  166.  
  167.  
  168. def expand(image, border = 0, fill = 0):
  169.     '''Add border to image'''
  170.     (left, top, right, bottom) = _border(border)
  171.     width = left + image.size[0] + right
  172.     height = top + image.size[1] + bottom
  173.     out = Image.new(image.mode, (width, height), _color(fill, image.mode))
  174.     out.paste(image, (left, top))
  175.     return out
  176.  
  177.  
  178. def fit(image, size, method = Image.NEAREST, bleed = 0, centering = (0.5, 0.5)):
  179.     '''
  180.     This method returns a sized and cropped version of the image,
  181.     cropped to the aspect ratio and size that you request.
  182.     '''
  183.     if type(centering) != type([]):
  184.         centering = [
  185.             centering[0],
  186.             centering[1]]
  187.     
  188.     if centering[0] > 1 or centering[0] < 0:
  189.         centering[0] = 0.5
  190.     
  191.     if centering[1] > 1 or centering[1] < 0:
  192.         centering[1] = 0.5
  193.     
  194.     if bleed > 0.49999 or bleed < 0:
  195.         bleed = 0
  196.     
  197.     bleedPixels = (int(float(bleed) * float(image.size[0]) + 0.5), int(float(bleed) * float(image.size[1]) + 0.5))
  198.     liveArea = (bleedPixels[0], bleedPixels[1], image.size[0] - bleedPixels[0] - 1, image.size[1] - bleedPixels[1] - 1)
  199.     liveSize = (liveArea[2] - liveArea[0], liveArea[3] - liveArea[1])
  200.     liveAreaAspectRatio = float(liveSize[0]) / float(liveSize[1])
  201.     aspectRatio = float(size[0]) / float(size[1])
  202.     if liveAreaAspectRatio >= aspectRatio:
  203.         cropWidth = int(aspectRatio * float(liveSize[1]) + 0.5)
  204.         cropHeight = liveSize[1]
  205.     else:
  206.         cropWidth = liveSize[0]
  207.         cropHeight = int(float(liveSize[0]) / aspectRatio + 0.5)
  208.     leftSide = int(liveArea[0] + float(liveSize[0] - cropWidth) * centering[0])
  209.     if leftSide < 0:
  210.         leftSide = 0
  211.     
  212.     topSide = int(liveArea[1] + float(liveSize[1] - cropHeight) * centering[1])
  213.     if topSide < 0:
  214.         topSide = 0
  215.     
  216.     out = image.crop((leftSide, topSide, leftSide + cropWidth, topSide + cropHeight))
  217.     return out.resize(size, method)
  218.  
  219.  
  220. def flip(image):
  221.     '''Flip image vertically'''
  222.     return image.transpose(Image.FLIP_TOP_BOTTOM)
  223.  
  224.  
  225. def grayscale(image):
  226.     '''Convert to grayscale'''
  227.     return image.convert('L')
  228.  
  229.  
  230. def invert(image):
  231.     '''Invert image (negate)'''
  232.     lut = []
  233.     for i in range(256):
  234.         lut.append(255 - i)
  235.     
  236.     return _lut(image, lut)
  237.  
  238.  
  239. def mirror(image):
  240.     '''Flip image horizontally'''
  241.     return image.transpose(Image.FLIP_LEFT_RIGHT)
  242.  
  243.  
  244. def posterize(image, bits):
  245.     '''Reduce the number of bits per color channel'''
  246.     lut = []
  247.     mask = ~(2 ** (8 - bits) - 1)
  248.     for i in range(256):
  249.         lut.append(i & mask)
  250.     
  251.     return _lut(image, lut)
  252.  
  253.  
  254. def solarize(image, threshold = 128):
  255.     '''Invert all values above threshold'''
  256.     lut = []
  257.     for i in range(256):
  258.         if i < threshold:
  259.             lut.append(i)
  260.             continue
  261.         lut.append(255 - i)
  262.     
  263.     return _lut(image, lut)
  264.  
  265.